package com.outstanding.ifml.warpper;

import java.util.ArrayList;
import java.util.List;

import com.outstanding.ifml.exception.IFMLException;

/**
 * ifml词法分析的载体，整个词法的逻辑都依赖该类
 * 
 * @author Chenzx
 *
 */
public class Liner {

	// 行数
	private int num;

	public int getNum() {
		return num;
	}

	public Liner(int num) {
		this.num = num;
	}

	private Token currentToken = new StartToken();

	private Token lastToken = null;
	// 行首空格技术
	private int sp = 0;

	// 行首开始就是不间断空格标示
	private boolean uninterrupted = true;

	// 变量
	private Identifier identifier;

	public int getSpNum() {
		return sp;
	}

	public Identifier getIdentifier() {
		return identifier;
	}

	public Comment getComment() {
		return comment;
	}

	public List<Prop> getProps() {
		return props;
	}

	public Value getValue() {
		return value;
	}

	private Comment comment;

	private List<Prop> props;

	private Value value;

	// 行的内容
	private StringBuffer content = new StringBuffer();

	public Token getCurrentToken() {
		return currentToken;
	}

	private boolean valid = true;

	// 是不是有效的行,即有标示fu
	public boolean isValid() {
		return this.valid && this.identifier != null;
	}

	public void setCurrentToken(Token t) {
		if (t == null) {
			throw new IFMLException("4001", "非法操作");
		}

		this.currentToken.complete();
		// t = this.currentToken.transformNextToken(t);
		if (this.currentToken.getType() == TokenType.IDENTIFIER) {
			this.identifier = (Identifier) this.currentToken;
		} else if (this.currentToken.getType() == TokenType.VALUE) {
			this.value = (Value) this.currentToken;
		} else if (this.currentToken.getType() == TokenType.COMMENT) {
			this.comment = (Comment) this.currentToken;
		} else if (this.currentToken.getType() == TokenType.PROP) {
			if (props == null)
				props = new ArrayList<Prop>();
			props.add((Prop) this.currentToken);

		}
		// 最开始的情况。
		else {
			if (this.currentToken.getType() == TokenType.START) {
				this.uninterrupted = false;
				if (t.getType() == TokenType.END) {
					this.valid = false;
				}
			}
		}
		this.currentToken = t;

	}

	/**
	 * 记录行首空格
	 */
	public void countFirstBlank() {
		if (this.uninterrupted)
			this.sp++;
	}

	public void putChar(char c) {
		content.append(c);
	}
	
	public void lastChar() {
		content.deleteCharAt(content.length()-1);
	}

	public String getContent() {
		StringBuffer sb = new StringBuffer("第"+this.num+"行，内容：");
		sb.append(content);
		return sb.toString();
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		if (this.identifier != null) {
			sb.append("the identifier=");
			sb.append(this.identifier.literals);
			sb.append(' ');
		}
		if (this.value != null) {
			sb.append("the value=");
			sb.append(this.value.literals);
			sb.append(' ');
		}

		if (this.comment != null) {
			sb.append("the comment=");
			sb.append(this.comment.literals);
			sb.append(' ');
		}
		if (this.props != null) {
			for (Prop prop : this.props) {
				sb.append("the prop.identifier=");
				sb.append(prop.getIdentifier().getLiteral());
				sb.append(' ');
				if (prop.getValue() != null) {
					sb.append("the prop.value=");
					sb.append(prop.getValue().getLiteral());
					sb.append(' ');
				}
				// List<Value> list = prop.getValueArray();
				// if(list!= null){
				// for(Value va:list){
				// sb.append("the prop.value=");
				// sb.append(va.getLiteral());
				// sb.append(' ');
				// }
				// }
			}
		}
		return sb.toString();
	}

	private Liner farther;
	private List<Liner> subliner = new ArrayList<Liner>();

	public void setFarther(Liner farther) {
		this.farther = farther;
	}

	public void addSubliner(Liner liner) {
		this.subliner.add(liner);
	}

	public Liner getFarther() {
		return farther;
	}

	public List<Liner> getSubliner() {
		return subliner;
	}
}
